package org.dandelero.properties.framework;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A predicate that filters property keys (strings) with a specific prefix and a numbered suffix.<br>
 * For example, application.users.count.1=some_value
 * 
 * @author dandelero
 */
public class KeyStartsWithNumberedSuffixPredicate extends KeyStartsWithPredicate {

	// ==================================================
	// Initialisation.
	// ==================================================

	/**
	 * Constructs a predicate that checks whether a string starts with a desired prefix.
	 * 
	 * @param desiredPrefix
	 *            the acceptable prefix.
	 */
	public KeyStartsWithNumberedSuffixPredicate(String desiredPrefix) {
		super(desiredPrefix);
	}

	// ==================================================
	// AbstractStringPredicate implementation.
	// ==================================================

	/**
	 * The check as to whether a property should be accepted or rejected.
	 * 
	 * @param s
	 *            the key.
	 * @return true or false.
	 */
	public boolean evaluate(String s) {
		if (s == null) {
			return false;
		}
		
		Pattern pattern = Pattern.compile(".+[.]\\d+$");
		Matcher matcher = pattern.matcher(s);
		return super.evaluate(s) && matcher.matches();
	}

}
